package cn.blossoming.controller.web;

import cn.blossoming.controller.vo.ApiResult;
import cn.blossoming.controller.vo.tips.TipsListVo;
import cn.blossoming.controller.vo.user.UserListVo;
import cn.blossoming.controller.vo.user.UserVo;
import cn.blossoming.controller.vo.video.VideoListVo;
import cn.blossoming.service.dto.Tips;
import cn.blossoming.service.dto.User;
import cn.blossoming.service.dto.Video;
import cn.blossoming.service.tips.TipsService;
import cn.blossoming.service.user.UserService;
import cn.blossoming.service.video.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/users")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private TipsService tipsService;


    @GetMapping(path = "/{id}/video")
    public ApiResult<VideoListVo> getVideoList(@PathVariable int id,
                                               @RequestParam(name = "page", required = false, defaultValue = "0") int page,
                                               @RequestParam(name = "per_page", required = false, defaultValue = "10") int PrePage) {
        List<Video> videoList = videoService.findPageByUser(id,new PageRequest(page, PrePage, new Sort(Sort.Direction.DESC, "id")));
        VideoListVo vo = new VideoListVo();
        vo.setVideoList(videoList);
        return ApiResult.builder(vo);
    }

    @GetMapping(path = "/{id}/tips")
    public ApiResult<TipsListVo> getTipsList(@PathVariable int id,
                                             @RequestParam(name = "page", required = false, defaultValue = "0") int page,
                                             @RequestParam(name = "per_page", required = false, defaultValue = "10") int PrePage) {
        List<Tips> tipsList = tipsService.findPageByUser(id, new PageRequest(page, PrePage, new Sort(Sort.Direction.DESC, "id")));
        TipsListVo vo = new TipsListVo();
        vo.setTipsList(tipsList);
        return ApiResult.builder(vo);
    }

    @GetMapping(path = "/fans")
    public ApiResult<UserListVo> getFansList(@RequestParam(name = "page", required = false, defaultValue = "0") int page,
                                             @RequestParam(name = "per_page", required = false, defaultValue = "10") int PrePage) {
        List<User> fans = userService.findFans(page, PrePage);
        UserListVo listVo = new UserListVo();
        listVo.setUserList(fans);
        return ApiResult.builder(listVo);
    }

    @GetMapping(path = "/follow")
    public ApiResult<UserListVo> getFollowList(@RequestParam(name = "page", required = false, defaultValue = "0") int page,
                                               @RequestParam(name = "per_page", required = false, defaultValue = "10") int PrePage) {
        List<User> fans = userService.findFollow(page, PrePage);
        UserListVo listVo = new UserListVo();
        listVo.setUserList(fans);
        return ApiResult.builder(listVo);
    }

    @GetMapping(path = "/{id}")
    public ApiResult<UserVo> getUserById(@PathVariable int id) {
        User user = userService.getUserById(id);
        UserVo userVo = new UserVo();
        userVo.setUser(user);
        return ApiResult.builder(userVo);
    }

    @PostMapping(path = "/follow/{id}")
    public ApiResult<?> addFollowUser(@PathVariable int id){
        userService.addFollowUser(id);
        return ApiResult.builder(null);
    }

    @DeleteMapping(path = "/follow/{id}")
    public ApiResult<?> delFollowUser(@PathVariable int id){
        userService.delFollowUser(id);
        return ApiResult.builder(null);
    }
}
